/*
 * ComponentContextBase.java
 *
 * Created on September 6, 2006, 12:09 PM
 *
 * (C) 2006 R. Alexander Milowski
 */

package org.infoset.component;

import java.net.URI;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;
import org.infoset.xml.DocumentLoader;
import org.infoset.xml.Infoset;
import org.infoset.xml.InfosetFactory;
import org.infoset.xml.Name;
import org.infoset.xml.NamespaceScope;
import org.infoset.xml.sax.SAXDocumentLoader;

/**
 *
 * @author alex
 */
public class ComponentContextBase implements Component.Context {

   Component.Context parentContext;
   Logger log;
   URI baseURI;
   Map<Name,Object> parameters;
   DocumentLoader loader;
   Infoset infoset;
   NamespaceScope scope;
   
   /**
    * Creates a new instance of ComponentContextBase
    */
   public ComponentContextBase(Component.Context context) {
      this(context,null,null);
   }
   public ComponentContextBase(URI baseURI,NamespaceScope scope) {
      this(null,baseURI,scope);
   }
   public ComponentContextBase(Component.Context context,URI baseURI,NamespaceScope scope) {
      this.parentContext = context;
      this.log = parentContext==null ? Logger.getLogger(ComponentContextBase.class.getName()) : parentContext.getLog();
      this.baseURI = baseURI==null ? parentContext==null ? null : parentContext.getBaseURI() : baseURI;
      this.parameters = null;
      this.loader = parentContext==null ? new SAXDocumentLoader() : parentContext.getDocumentLoader() ;
      this.infoset = parentContext==null ? InfosetFactory.getDefaultInfoset() : parentContext.getInfoset();
      this.scope = scope==null ? (context==null ? null : context.getNamespaceScope()) : scope;
   }
   
   public Logger getLog() {
      return log;
   }
   
   public void setLog(Logger log) {
      this.log = log;
   }
   
   public NamespaceScope getNamespaceScope() {
      return scope;
   }
   
   public void setParameter(Name name,Object value) {
      if (parameters==null) {
         parameters = new HashMap<Name,Object>();
      }
      parameters.put(name,value);
   }
   
   public Object getParameter(Name name) {
      Object value = null;
      if (parameters!=null) {
         value = parameters.get(name);
      } 
      if (value==null && parentContext!=null) {
         value = parentContext.getParameter(name);
      }
      return value;
   }
   
   public Collection<Name> getParameterNames() {
      Set<Name> names = new HashSet<Name>();
      if (parameters!=null) {
         names.addAll(parameters.keySet());
      }
      if (parentContext!=null) {
         names.addAll(parentContext.getParameterNames());
      }
      return names;
   }
   
   public URI getBaseURI() {
      return baseURI;
   }

   /**
    * Returns the current document loader for the component
    */
   public DocumentLoader getDocumentLoader() {
      return loader;
   }

   /**
    * Returns the current infoset for the component
    */
   public Infoset getInfoset() {
      return infoset;
   }
}
